#!/bin/bash
#
# Usage: get_mol_classes molecule.pdb [Path to Mol_classes files]
#
# Get numbers of the atoms of different classes in the molecule.

if [ $# -lt 2 ]; then
	P=.
else
	P=$2
fi

coors2pat $1 $P/sigmas.dat > tmp0.pat
cat tmp0.pat | sed 's/Cl/H/g' > tmp1.pat
assignAtomTypes tmp1.pat $P/mol_classes.ff > tmp1.at
assignAtomTypes tmp0.pat $P/dummy.ff > tmp0.at

classes="alkane alkene diene alkine benzene_ring alkohol aldehyde ketone COOH ether"

declare -A NAtom

for class in $classes
do 
NAtom[$class]=$(cat tmp1.at | grep C_$class | wc -l)

[ ${NAtom[$class]} == 0 ]&& NAtom[$class]=$(cat tmp1.at | grep O_$class | wc -l);

#echo [$class]: ${NAtom[$class]} ${NAtom["alkane"]}
done

declare -A Fragment


Fragment[DoubleBond]=$(( ${NAtom[alkene]}/2+${NAtom[diene]} ))
Fragment[TripleBond]=$(( ${NAtom[alkine]}/2))
Fragment[Benzyl]=$(( ${NAtom[benzene_ring]}/6))
Fragment[Aldehyde]=${NAtom[aldehyde]}
Fragment[Alcohol]=${NAtom[alkohol]}
Fragment[Ketone]=${NAtom[ketone]}
Fragment[Carboxyl]=${NAtom[COOH]}
Fragment[Ether]=${NAtom[ether]}

Fragments="DoubleBond TripleBond Benzyl Aldehyde Alcohol Ketone Carboxyl Ether "

TotalFragments=0

#echo NAtom[alkane]=${NAtom[alkane]}

if [ ${NAtom[alkane]} -gt 0 ]; then
	LastFragment='Alkane'
else 
	LastFragment='unknown compound'
fi

for fragment in $Fragments
do
#	echo $fragment
	if [ ${Fragment[$fragment]} -gt 0 ]; then
		TotalFragments=$((TotalFragments+1))
		LastFragment=$fragment	
	fi

done


if [ $TotalFragments -gt 1 ]; then
	class=polyfragment
else
	class=$LastFragment
fi

NCl=$(cat tmp0.at | grep Cl_atom | wc -l) 

if [ $NCl -gt 0 ]; then
	clorinated=yes
else
	clorinated=no
fi


echo Class: $class
echo Chlorinated: $clorinated

echo 
echo ------------ Components ------------
echo
for fragment in $Fragments
do
	echo $fragment: ${Fragment[$fragment]}

done

echo Clorine: $NCl
